package com.huawei.appmarket.service.deamon.download.remote;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import com.huawei.appgallery.downloadengine.api.SessionDownloadTask;
import com.huawei.appgallery.foundation.service.common.protocol.AppDetailActivityProtocol;
import com.huawei.appgallery.foundation.service.common.protocol.AppDetailActivityProtocol.Request;
import com.huawei.appmarket.service.appmgr.view.activity.AppManagerProtocol;
import com.huawei.appmarket.service.deamon.bean.DownloadHistory;
import com.huawei.appmarket.service.externalapi.view.ThirdApiActivityWithTitle;
import com.huawei.updatesdk.service.otaupdate.UpdateKey;
import o.cdb;
import o.cdf;
import o.cdg;
import o.cjk;
import o.cjm;
import o.cjo;
import o.coc;
import o.dgh;
import o.diu;
import o.Logger;
import o.doo;
import o.ContentManager;
import o.drj;
import o.dsl;
import o.dsw;
import o.dvq;
import o.dvt;
import o.dwc;
import o.dwk;
import o.euc;

public class DownloadAgentService extends Service {
    public static final String COMMAND_ARG = "command";
    public static final String PACKAGE_NMAE_ARG = "packagename";
    public static final int PARAM_ERROR = -1;
    public static final int RESUME_ERROR = -2;
    private static final String ˎ = (ContentManager.getInstant().getContext().getPackageName() + ".service.DownloadAgent.StatusReport");
    private volatile Looper ˏ;
    private volatile ServiceHandler ॱ;

    final class ServiceHandler extends Handler {
        final /* synthetic */ DownloadAgentService ˊ;

        public ServiceHandler(DownloadAgentService downloadAgentService, Looper looper) {
            this.ˊ = downloadAgentService;
            super(looper);
        }

        public void handleMessage(Message message) {
            this.ˊ.onHandleIntent((Intent) message.obj, message.arg1);
        }
    }

    public static void doCommand(String str, int i) {
        Intent intent = new Intent();
        intent.putExtra(PACKAGE_NMAE_ARG, str);
        intent.putExtra(COMMAND_ARG, i);
        intent.setClass(ContentManager.getInstant().getContext(), DownloadAgentService.class);
        ContentManager.getInstant().getContext().startService(intent);
    }

    public static void setResult(Context context, int i, Intent intent) {
        intent.setAction(ˎ);
        intent.putExtra("returncode", i);
        context.sendBroadcast(intent, dvq.ॱ());
    }

    public static void sendPauseMessage(String str) {
        SessionDownloadTask sessionDownloadTask = new SessionDownloadTask();
        sessionDownloadTask.ʼ(str);
        sessionDownloadTask.ॱॱ(6);
        dvq.ˎ(ContentManager.getInstant().getContext(), sessionDownloadTask);
    }

    public void onCreate() {
        super.onCreate();
        HandlerThread handlerThread = new HandlerThread("IntentService[RemoteDownloadService]");
        handlerThread.start();
        this.ˏ = handlerThread.getLooper();
        if (this.ˏ != null) {
            this.ॱ = new ServiceHandler(this, this.ˏ);
        } else {
            this.ॱ = new ServiceHandler(this, ContentManager.getInstant().getContext().getMainLooper());
        }
    }

    public void onStart(Intent intent, int i) {
        Message obtainMessage = this.ॱ.obtainMessage();
        obtainMessage.arg1 = i;
        obtainMessage.obj = intent;
        this.ॱ.sendMessage(obtainMessage);
    }

    public int onStartCommand(Intent intent, int i, int i2) {
        Logger.ˎ("DownloadAgentService", "downloadAgentService onStartCommand");
        onStart(intent, i2);
        return 2;
    }

    public void onDestroy() {
        super.onDestroy();
        this.ˏ.quit();
    }

    public IBinder onBind(Intent intent) {
        return null;
    }

    protected void onHandleIntent(Intent intent, int i) {
        if (intent != null) {
            String stringExtra = intent.getStringExtra(PACKAGE_NMAE_ARG);
            int intExtra = intent.getIntExtra(COMMAND_ARG, -1);
            if (TextUtils.isEmpty(stringExtra)) {
                Logger.e("DownloadAgentService", "packageName error:" + stringExtra);
                setResult(this, -1, new Intent());
                stopSelf(i);
                return;
            }
            ˏ(i, stringExtra, intExtra, intent);
        }
    }

    private void ˏ(int i, String str, int i2, Intent intent) {
        Logger.ˎ("DownloadAgentService", "receive download command, packageName:" + str + ",command:" + i2);
        Intent intent2;
        if (i2 != 2 || diu.ˊ().ˎ()) {
            intent2 = new Intent();
            intent2.putExtra(PACKAGE_NMAE_ARG, str);
            switch (i2) {
                case 1:
                    SessionDownloadTask ˋ = dvt.ˏ().ˋ(str);
                    if (ˋ != null) {
                        dvt.ˏ().ˏ(ˋ.ʾ());
                    }
                    setResult(this, 0, intent2);
                    break;
                case 2:
                    ˏ(intent2, str);
                    break;
                case 3:
                    ˏ(this, intent2, str);
                    break;
                case 5:
                    ˊ(intent2, str);
                    break;
                default:
                    setResult(this, -1, intent2);
                    Logger.e("DownloadAgentService", "unknow command:" + i2);
                    break;
            }
            stopSelf(i);
            return;
        }
        if (dvt.ˏ().ˋ(str) != null) {
            intent2 = new Intent(this, ThirdApiActivityWithTitle.class);
            intent2.setAction("com.huawei.appmarket.intent.action.launcher.downloadmanager");
            intent2.setFlags(268435456);
            if (intent != null) {
                intent2.putExtras(intent);
            }
            ContentManager.getInstant().getContext().startActivity(intent2);
            stopSelf(i);
        }
        sendPauseMessage(str);
    }

    private void ˏ(Intent intent, String str) {
        SessionDownloadTask ˋ = dvt.ˏ().ˋ(str);
        if (ˋ == null) {
            euc.ˊ().ॱ();
            DownloadHistory ˏ = dsw.ˋ().ˏ(str);
            if (ˏ != null) {
                ˋ(intent, ˏ, str);
            } else {
                setResult(this, -1, intent);
            }
            euc.ˊ().ˏ();
        } else if (doo.ʻ(ContentManager.getInstant().getContext()) || (doo.ॱॱ(ContentManager.getInstant().getContext()) && doo.ᐝ(ContentManager.getInstant().getContext()))) {
            sendPauseMessage(str);
            String ˊ = dwc.ॱ().ˊ(str);
            cjo appManagerProtocol;
            cjm o_cjm;
            if (ˋ.ˌ() == 4) {
                appManagerProtocol = new AppManagerProtocol();
                appManagerProtocol.ˊ().ॱ(true);
                appManagerProtocol.ˊ().ॱ(ˊ);
                o_cjm = new cjm("installmgr.activity", appManagerProtocol);
                o_cjm.ॱ(this).addFlags(268468224);
                cjk.ˎ().ˋ(this, o_cjm);
                return;
            }
            appManagerProtocol = new AppDetailActivityProtocol();
            Request request = new Request(drj.ॱ(str), null);
            request.ˎ(ˊ);
            appManagerProtocol.ˏ(request);
            o_cjm = new cjm("appdetail.activity", appManagerProtocol);
            o_cjm.ॱ(this).addFlags(268468224);
            cjk.ˎ().ˋ(this, o_cjm);
        } else {
            intent.putExtra(UpdateKey.STATUS, ˋ.ˈ());
            if (dvt.ˏ().ˎ(ˋ)) {
                setResult(this, 0, intent);
            } else {
                setResult(this, -2, intent);
            }
        }
    }

    private void ˏ(Context context, Intent intent, String str) {
        new dwk().ॱ(str);
        ((cdf) cdb.ॱ(cdf.class)).ॱ(str);
        setResult(context, 0, intent);
    }

    private void ˊ(Intent intent, String str) {
        int ˋ = ((cdg) cdb.ॱ(cdg.class)).ˋ(str);
        if (ˋ == 2 || ˋ == 1) {
            ˊ(str);
        } else if (ˋ == 10 || ˋ == 11) {
            Logger.ˎ("DownloadAgentService", "app is installing:" + str + "-" + ˋ);
        } else {
            Logger.e("DownloadAgentService", "app is not valid status:" + str);
            setResult(this, -1, intent);
        }
    }

    private void ˊ(String str) {
        Logger.ˎ("DownloadAgentService", "start app install:" + str);
        dsl.ˊ(str, "", "", 0, coc.ˎ);
        dgh.ॱ(ContentManager.getInstant().getContext(), "990502", str);
    }

    private void ˋ(Intent intent, DownloadHistory downloadHistory, String str) {
        int ˋ = ((cdg) cdb.ॱ(cdg.class)).ˋ(str);
        if (ˋ == 10 || ˋ == 11) {
            Logger.ˎ("DownloadAgentService", "app is installing:" + str);
            return;
        }
        dsl.ˊ(downloadHistory.ˋ(), downloadHistory.ˏॱ(), downloadHistory.ˊॱ(), downloadHistory.ʼ(), coc.ˎ);
        setResult(this, 0, intent);
    }
}
